package com.android.lovingpet.adopt.adoption.adopt;

import android.os.Bundle;
import android.support.annotation.Nullable;

import com.android.lovingpet.base.BasePresenter;
import com.android.lovingpet.bean.Account;
import com.android.lovingpet.bean.Body;
import com.android.lovingpet.bean.Team;
import com.android.lovingpet.common.Back;
import com.android.lovingpet.common.FileModel;
import com.android.lovingpet.common.UserModel;
import com.android.lovingpet.util.LocationClient;

import java.util.HashMap;

/**
 * Created by ikata on 2018/4/27.
 */

public class AdoptPetPresenter extends BasePresenter<IAdoptPetContract.View>implements IAdoptPetContract.Presenter {

    public static final int FLAG_PUBLISHER=0;
    public static final int FLAG_CITY=1;

    @Override
    public void getPersonMaster(int masterId) {
        UserModel.getInstance().getInfo(masterId, new FileModel.FileBack() {
            @Override
            public void loadSuccess(String url) {

            }

            @Override
            public void onFailure(String msg) {
                if (getView()==null)
                    return;
                getView().showToast(msg);
            }

            @Override
            public void onSuccess(@Nullable Bundle bundle) {
                if (bundle==null)
                    return;
                Account account=bundle.getParcelable("account");
                if (account==null)
                    return;
                if (getView()==null)
                    return;
                Bundle bundle1=new Bundle();
                bundle1.putInt("flag",FLAG_PUBLISHER);
                bundle1.putString("data",account.getName());
                getView().next(bundle1);
            }
        });
    }

    public void getTeamMaster(int id)
    {
        UserModel.getInstance().getTeamInfo(id, new FileModel.FileBack() {
            private Team team;
            @Override
            public void loadSuccess(String url) {
                if (team==null)
                    return;
                if (getView()==null)
                    return;
                team.setImage(url);
                getView().setTeam(team);
            }

            @Override
            public void onFailure(String msg) {
                if (getView()==null)
                    return;
                getView().showToast(msg);
            }

            @Override
            public void onSuccess(@Nullable Bundle bundle) {
                if (bundle==null)
                    return;

                team=bundle.getParcelable("team");

                Team team=bundle.getParcelable("team");
                if (team==null)
                    return;
                if (getView()==null)
                    return;

                Bundle bundle1=new Bundle();
                bundle1.putInt("flag",FLAG_PUBLISHER);
                bundle1.putString("data",team.getName());
                getView().next(bundle1);
            }
        });
    }

    public void getMasterInfo(int id)
    {
        getMasterType(id);
    }


    public void getMasterType(int id)
    {
        UserModel.getInstance().getType(id, new Back() {
            @Override
            public void onFailure(String msg) {
                if (getView() == null) {
                    return;
                }
                getView().showToast(msg);
            }

            @Override
            public void onSuccess(@Nullable Bundle bundle) {
                if (bundle==null)
                    return;
                int type =bundle.getInt("type");

               // getHostImage(id, type);
                if (type==UserModel.TYPE_TEAM)
                {
                    getTeamMaster(id);
                }
                if (type==UserModel.TYPE_PERSON)
                {
                    getPersonMaster(id);
                }
            }
        });
    }

    @Override
    public void getAddress(double longitude,double latitude) {
        HashMap<String,String> params=new HashMap<>();

        params.put("location",longitude+","+latitude);
        LocationClient.regeo(params, new Back() {
            @Override
            public void onFailure(String msg) {
                getView().showToast(msg);
            }

            @Override
            public void onSuccess(@Nullable Bundle bundle) {
                assert bundle != null;
                String address=bundle.getString("address");
                if (getView()==null) return;
                Bundle bundle1=new Bundle();
                bundle1.putInt("flag",FLAG_CITY);
                bundle1.putString("data",address);
                getView().next(bundle1);
            }
        });
    }



    private void getHostImage(int id, int userType) {
        FileModel.getInstance().downloadImage(id, userType, new FileModel.FileBack() {
            @Override
            public void loadSuccess(String url) {

            }

            @Override
            public void onFailure(String msg) {

            }

            @Override
            public void onSuccess(@Nullable Bundle bundle) {

            }
        });
    }
}
